package org.example;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Config {

    //电脑ip地址
    private String appIp;
    //应用端口
    private Integer appPort;
    //魔兽争霸3端口
    private Integer war3Port;
    //服务器ip地址
    private String serverIp;
    //服务器用来接收魔兽争霸3 udp数据端口
    private Integer serverUdpPort;
    //服务器用来接收魔兽争霸3 tcp数据端口
    private Integer serverTcpPort;

    public static Config instance;

    static {
        Properties prop = new Properties();
        FileInputStream fileInputStream = null;
        InputStream inputStream = null;
        try {

            fileInputStream = new FileInputStream("config.properties");

        } catch (Exception ignored) {

        }

        try {
            if (fileInputStream != null) {
                prop.load(fileInputStream);
            } else {
                inputStream = Config.class.getClassLoader().getResourceAsStream("config.properties");
                prop.load(inputStream);
            }

            String appIp = prop.getProperty("app.ip");
            Integer appPort = Integer.valueOf(prop.getProperty("app.port"));
            Integer war3Port = Integer.valueOf(prop.getProperty("war3.port"));
            String serverIp = prop.getProperty("server.ip");
            Integer serverUdpPort = Integer.valueOf(prop.getProperty("server.udp.port"));
            Integer serverTcpPort = Integer.valueOf(prop.getProperty("server.tcp.port"));

            instance = new Config(appIp, appPort, war3Port, serverIp, serverUdpPort, serverTcpPort);
            System.out.println("config.properties 配置文件加载成功:");
            System.out.println(instance);

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("config.properties 配置文件加载异常");
        }finally {

            if(fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }



    }

    public Config(String appIp, Integer appPort, Integer war3Port, String serverIp, Integer serverUdpPort, Integer serverTcpPort) {
        this.appIp = appIp;
        this.appPort = appPort;
        this.war3Port = war3Port;
        this.serverIp = serverIp;
        this.serverUdpPort = serverUdpPort;
        this.serverTcpPort = serverTcpPort;
    }


    public String getAppIp() {
        return appIp;
    }

    public void setAppIp(String appIp) {
        this.appIp = appIp;
    }

    public Integer getAppPort() {
        return appPort;
    }

    public void setAppPort(Integer appPort) {
        this.appPort = appPort;
    }

    public Integer getWar3Port() {
        return war3Port;
    }

    public void setWar3Port(Integer war3Port) {
        this.war3Port = war3Port;
    }

    public String getServerIp() {
        return serverIp;
    }

    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    public Integer getServerUdpPort() {
        return serverUdpPort;
    }

    public void setServerUdpPort(Integer serverUdpPort) {
        this.serverUdpPort = serverUdpPort;
    }

    public Integer getServerTcpPort() {
        return serverTcpPort;
    }

    public void setServerTcpPort(Integer serverTcpPort) {
        this.serverTcpPort = serverTcpPort;
    }

    @Override
    public String toString() {
        return "Config{" +
                "appIp='" + appIp + '\'' +
                ", appPort=" + appPort +
                ", war3Port=" + war3Port +
                ", serverIp='" + serverIp + '\'' +
                ", serverUdpPort=" + serverUdpPort +
                ", serverTcpPort=" + serverTcpPort +
                '}';
    }
}
